#!d:\anaconda3\envs\office_automation\python.exe -u
# -*- coding=utf-8 -*-
r"""
author: git config IVEN_CN && git config 13377529851@QQ.com
Date: 2024-08-23 11:38:11 +0800
LastEditTime: 2024-08-23 13:43:35 +0800
FilePath: \office_automation\FWNU\UI.py
details: 构建查找哪些过审的图但是未使用的UI界面

Copyright (c) 2024 by IVEN, All Rights Reserved. 
"""
import PyQt5.QtWidgets as qw
from PyQt5.QtCore import pyqtSignal
try:
    from main import Convert, deal_img
except ImportError:
    from FWNU.main import Convert, deal_img


class Button(qw.QPushButton):
    """具有hover悬浮信号的按钮"""

    hovered = pyqtSignal()
    unhovered = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def enterEvent(self, event):
        self.hovered.emit()
        super().enterEvent(event)

    def leaveEvent(self, event):
        self.unhovered.emit()
        super().leaveEvent(event)


class FWNU_UI(qw.QWidget):
    path = None

    def __init__(self):
        super(FWNU_UI, self).__init__()
        self.setWindowTitle("FWNU")

        self.main_layout = qw.QVBoxLayout(self)

        # region 添加选择路径的按钮和标签
        path_layout = qw.QHBoxLayout()
        self.path_button = Button("选择路径")
        self.path_button.setFixedWidth(80)
        self.path_button.clicked.connect(self.select_path)
        self.path_button.hovered.connect(self.path_button_hovered_callback)
        self.path_button.unhovered.connect(self.button_unhovered_callback)
        path_layout.addWidget(self.path_button)

        self.path_label = qw.QLabel("未选择路径")
        path_layout.addWidget(self.path_label)

        self.main_layout.addLayout(path_layout)
        # endregion

        # region 添加查找按钮
        self.find_button = Button("查找")
        self.main_layout.addWidget(self.find_button)
        self.find_button.hovered.connect(self.find_button_hovered_callback)
        self.find_button.unhovered.connect(self.button_unhovered_callback)
        self.find_button.clicked.connect(self.find_button_clicked_callback)
        # endregion

        # region 添加文本域
        self.text = qw.QTextEdit()
        self.text.setReadOnly(True)
        # 设置文本域高度
        self.text.setFixedHeight(200)
        self.text.setText("欢迎使用自动化工具,本工具用于查找哪些过审的图但是未使用")
        self.main_layout.addWidget(self.text)
        # endregion

    def select_path(self):
        options = qw.QFileDialog.Options()
        options |= qw.QFileDialog.ShowDirsOnly
        directory = qw.QFileDialog.getExistingDirectory(self, "选择文件夹", "", options)
        if directory:
            self.path_label.setText(directory)
            self.path = directory

    def path_button_hovered_callback(self):
        self.text.setText("选择路径: 选择出包含过审excel文件的文件夹")

    def button_unhovered_callback(self):
        self.text.setText("欢迎使用自动化工具,本工具用于查找哪些过审的图但是未使用")

    def find_button_hovered_callback(self):
        self.text.setText(
            "查找\n查找哪些过审的图但是未使用,程序运行过程会将xsl文件另存为xlsx文件，但是这些文件不会被删除，需要手动清理"
        )

    def convert(self):
        self.text.setText("正在转换xls文件为xlsx文件")
        _convert = Convert(self.path)
        _convert.xls2xlsx()

    def find(self):
        if not self.path:
            self.text.setText("未选择路径，这个路径是包含过审excel文件的文件夹")
        else:
            self.text.setText("正在查找")
            deal_img(self.path)

    def find_button_clicked_callback(self):
        self.convert()
        self.find()


if __name__ == "__main__":
    app = qw.QApplication([])
    fwnu_ui = FWNU_UI()
    fwnu_ui.show()
    app.exec_()
